package com.ptmall.app.ui.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.ptmall.app.R;
import com.ptmall.app.bean.AddressBean;
import com.ptmall.app.bean.TimeSelectBean;

import java.util.Calendar;
import java.util.List;
import java.util.TimeZone;


/**
 * Created by Administrator on 2018/1/12.
 */

public class TimeAdapter extends BaseAdapter {
    private static String mYear;
    private static String mMonth;
    private static String mDay;
    private static String mWay;

    public TimeAdapter() {
    }

    List<TimeSelectBean> list;

    public void pushData(List<TimeSelectBean> list){
        this.list = list;
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return 3;
    }

    Click click;

    public void setClick(Click click){
        this.click = click;
    }


    @Override
    public Object getItem(int position) {
        return 0;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (null == convertView) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_time, parent,false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        switch (position){
            case 0:
                viewHolder.time1.setText(time1(1));
                viewHolder.time2.setText(time2(1)+"明天");
                break;
            case 1:
                viewHolder.time1.setText(time1(2));
                viewHolder.time2.setText(time2(2)+"后天");
                break;
            case 2:
                viewHolder.time1.setText(time1(3));
                viewHolder.time2.setText(time2(3)+"大后天");
                break;
        }

        viewHolder.radioButton1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    click.Click(true,position);
                }
            }
        });
        viewHolder.radioButton2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    click.Click2(true,position);
                }
            }
        });

        viewHolder.radioButton1.setChecked(list.get(position).time1);
        viewHolder.radioButton2.setChecked(list.get(position).time2);

        return convertView;
    }

    class ViewHolder {
        RadioButton radioButton1,radioButton2;
        TextView time1,time2;

        ViewHolder(View view) {
            radioButton1 = (RadioButton) view.findViewById(R.id.radio1);
            radioButton2 = (RadioButton) view.findViewById(R.id.radio2);
            time1 = (TextView) view.findViewById(R.id.time1);
            time2 = (TextView) view.findViewById(R.id.time2);
        }
    }

    public  interface Click{
        void Click(boolean position,int i);
        void Click2(boolean position,int i);
    }

    public String time2(int i){
        final Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_MONTH, i);
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        mYear = String.valueOf(c.get(Calendar.YEAR)); // 获取当前年份
        mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 获取当前月份
        mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH));// 获取当前月份的日期号码
        mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
        if("1".equals(mWay)){
            mWay ="日";
        }else if("2".equals(mWay)){
            mWay ="一";
        }else if("3".equals(mWay)){
            mWay ="二";
        }else if("4".equals(mWay)){
            mWay ="三";
        }else if("5".equals(mWay)){
            mWay ="四";
        }else if("6".equals(mWay)){
            mWay ="五";
        }else if("7".equals(mWay)){
            mWay ="六";
        }
        return "周"+mWay;
    }
    public String time1(int i){
        final Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_MONTH, i);
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        mYear = String.valueOf(c.get(Calendar.YEAR)); // 获取当前年份
        mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 获取当前月份
        mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH));// 获取当前月份的日期号码
        mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
        if("1".equals(mWay)){
            mWay ="日";
        }else if("2".equals(mWay)){
            mWay ="一";
        }else if("3".equals(mWay)){
            mWay ="二";
        }else if("4".equals(mWay)){
            mWay ="三";
        }else if("5".equals(mWay)){
            mWay ="四";
        }else if("6".equals(mWay)){
            mWay ="五";
        }else if("7".equals(mWay)){
            mWay ="六";
        }
        return mYear+"-"+ mMonth + "-" + mDay;
    }

}
